/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.base.dao.impl;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Calendar;

import com.kingcore.director.base.dao.DirectorDaoJdbc;
import com.kingcore.framework.base.dao.impl.DaoJdbcPlainImpl;

/**
 * <p>java类文件的说明...</p>
 * @author Zeven/wzw on Mar 3, 2009
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class DirectorDaoJdbcImpl extends DaoJdbcPlainImpl implements DirectorDaoJdbc {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on Mar 4, 2011
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public static String toCSV(int[] ids) {

		final int size = ids.length;

		if (size < 1)
			return "";

		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < size; i++) {
			int obj = ids[i];
			sb.append(obj);
			if (i < size - 1)
				sb.append(",");
		}

		return sb.toString();
	}
	
	DateFormat df =  new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //yyyy-MM-dd
	protected String getSystemTime(){
		return df.format( Calendar.getInstance().getTime());
	}
	

	/**
	 * 
	 * @param key
	 * @param defaultValue
	 * @param tblName
	 * @param keyCol
	 * @param valCol
	 * @return
	 * @throws SQLException
	 */
	protected Object queryLargeObject(String key, String defaultValue,
			String tblName, String keyCol, String valCol) throws SQLException {

		Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
		try {
			String sql="Select Contents From "+tblName+" where "+keyCol+"="+key;
			con = this.getConnection(); 
            stmt = con.createStatement();
            stmt.execute(sql);
			log.debug(sql);
            rs = stmt.getResultSet();
            if  (rs != null ) {
                while ( rs.next() ) {
                    InputStream image = rs.getBinaryStream(1);
                    ObjectInputStream ois = null;
                    try {
                        ois = new ObjectInputStream(image);
            			log.debug("Now,return object.");
                        return ois.readObject();
					} catch (Exception e) {
						e.printStackTrace();  //第一次为空时候可能报错
						return null;
					}finally{
			        	try {
			        		if (ois!=null) {
								ois.close();
							}
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
                }
            }else{
            	stmt.execute("Insert Into "+tblName+"(Id) values("+key+")" );
            	con.commit();
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        	wzw.util.DbUtils.closeQuietly(con, stmt, rs);
        }

		return null;
	}
}
